package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.domain.RecruitmentTopDO;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.jzweb.service.RecruitmentTopService;
import com.jinzhi.jzweb.utils.IdCardUtil;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 
 * <pre>
 * 招聘置顶
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/recruitmentTop")
public class RecruitmentTopController extends BaseController {
	@Autowired
	private RecruitmentTopService recruitmentTopService;
	@Autowired
	private UserService userService;
	@Autowired
	private OrganizationNewsService organizationNewsService;
	@Autowired
	private DictService dictService;
	@Autowired
	private OrganizationRolesService organizationRolesService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:recruitmentTop:recruitmentTop")
	String RecruitmentTop(){
	    return "jzweb/recruitmentTop/recruitmentTop";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:recruitmentTop:recruitmentTop")
	public Result<Page<RecruitmentTopDO>> list(RecruitmentTopDO recruitmentTopDTO){
        Wrapper<RecruitmentTopDO> wrapper = new EntityWrapper<RecruitmentTopDO>().orderBy("id", false);
        wrapper.eq("state","0");

		String roleName = getRoleNames();//查询用户角色
		if (!roleName.contains("超级用户角色")) {
			wrapper.eq("org_id",this.getUser().getOrgId());
		}else {
			if (StringUtils.isNotBlank(recruitmentTopDTO.getOrgName())){
				Wrapper<OrganizationNewsDO> orWrapper = new EntityWrapper<OrganizationNewsDO>().like("name",recruitmentTopDTO.getOrgName());
				List<OrganizationNewsDO>  organizationNewsDOS = organizationNewsService.selectList(orWrapper);
				String str = "";
				for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS){
					str = str + organizationNewsDO.getId() + ",";
				}
				wrapper.and();
				if(StringUtils.isNotBlank(str)){
					wrapper.in("org_id",str.substring(0,str.length()-1));
				}else{
					wrapper.eq("org_id",null);
				}
			}
		}

        Page<RecruitmentTopDO> page = recruitmentTopService.selectPage(getPage(RecruitmentTopDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			RecruitmentTopDO obj = (RecruitmentTopDO) page.getRecords().get(i);
			if (obj.getUserId() != null){
				UserDO userDO = userService.selectById(obj.getUserId());
				if (userDO != null) {
					obj.setUserName(userDO.getName());
				}
			}
			if (obj.getOrgId() != null){
				OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
				if (organizationNewsDO != null) {
					obj.setOrgName(organizationNewsDO.getName());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:recruitmentTop:add")
	String add(){
	    return "jzweb/recruitmentTop/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:recruitmentTop:edit")
	String edit(@PathVariable("id") Long id,Model model){
		RecruitmentTopDO recruitmentTop = recruitmentTopService.selectById(id);
		model.addAttribute("recruitmentTop", recruitmentTop);
	    return "jzweb/recruitmentTop/edit";
	}
	
	@Log("添加招聘置顶")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:recruitmentTop:add")
	public Result<String> save( RecruitmentTopDO recruitmentTop){
		recruitmentTopService.insert(recruitmentTop);
        return Result.ok();
	}
	
	@Log("修改招聘置顶")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:recruitmentTop:edit")
	public Result<String>  update( RecruitmentTopDO recruitmentTop){
		boolean update = recruitmentTopService.updateById(recruitmentTop);
		return update ? Result.ok() : Result.fail();
	}

	@GetMapping("/isTop")
	@RequiresPermissions("jzweb:recruitmentTop:add")
	String isTop(Model model){
		Double money = 10.00;
		Map<String, String> maps = new HashMap<>();
		maps.put("name", "置顶金额");
		maps.put("type", "top_money");
		String topMoney = dictService.selDicBy(maps);
		if (StringUtils.isNotBlank(topMoney)) {
			//判断是否为数字或小数
			if (IdCardUtil.isNumericDou(topMoney)) {
				money = Double.parseDouble(topMoney);
			}
		}
		model.addAttribute("money",money);

		String roleName = getRoleNames();//查询用户角色
		if (!roleName.contains("超级用户角色")) {
			Map map = new HashMap();
			map.put("id",this.getUser().getOrgId());
			map.put("name","--");
			if(this.getUser().getOrgId() != null){
				OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
				if(organizationNewsDO != null){
					map.put("name",organizationNewsDO.getName());
				}
			}
			model.addAttribute("type",1);
			model.addAttribute("orgRole",map);
		}else {
			model.addAttribute("type",0);
			model.addAttribute("orgRole", organizationRolesService.selByRoleType(3));
		}
		return "jzweb/recruitmentTop/isTop";
	}
	
	@Log("删除招聘置顶")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:recruitmentTop:remove")
	public Result<String>  remove( Long id){
		recruitmentTopService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除招聘置顶")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:recruitmentTop:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		recruitmentTopService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
